<template>
  <div class="questionnaire-fill">
    <el-card v-loading="loading">
      <!-- 问卷头部 -->
      <div class="header">
        <h1 class="title">{{ questionnaire.title }}</h1>
        <p class="description">{{ questionnaire.description }}</p>
        <div class="divider"></div>
      </div>

      <!-- 问卷表单 -->
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="top"
        class="question-form"
      >
        <template v-for="(question, index) in questions" :key="question.id">
          <el-card class="question-card" shadow="hover">
            <div class="question-header">
              <span class="question-index">Q{{ index + 1 }}</span>
              <span class="question-title">
                {{ question.title }}
                <span class="required" v-if="question.required">*</span>
              </span>
            </div>

            <!-- 单选题 -->
            <el-form-item
              v-if="question.type === 1"
              :prop="`answers.${question.id}`"
              :rules="question.required ? { required: true, message: '请选择答案', trigger: 'change' } : {}"
            >
              <el-radio-group v-model="form.answers[question.id]" class="option-group">
                <el-radio
                  v-for="option in question.options"
                  :key="option.id"
                  :label="option.content"
                  class="option-item"
                  border
                >
                  {{ option.content }}
                </el-radio>
              </el-radio-group>
            </el-form-item>

            <!-- 多选题 -->
            <el-form-item
              v-else-if="question.type === 2"
              :prop="`answers.${question.id}`"
              :rules="question.required ? { required: true, message: '请选择答案', trigger: 'change' } : {}"
            >
              <el-checkbox-group v-model="form.answers[question.id]" class="option-group">
                <el-checkbox
                  v-for="option in question.options"
                  :key="option.id"
                  :label="option.content"
                  class="option-item"
                  border
                >
                  {{ option.content }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <!-- 填空题 -->
            <el-form-item
              v-else-if="question.type === 3"
              :prop="`answers.${question.id}`"
              :rules="question.required ? { required: true, message: '请填写答案', trigger: 'blur' } : {}"
            >
              <el-input
                v-model="form.answers[question.id]"
                type="textarea"
                :rows="3"
                placeholder="请输入您的答案"
                resize="none"
              />
            </el-form-item>
          </el-card>
        </template>

        <!-- 提交按钮 -->
        <div class="form-footer">
          <el-button type="primary" size="large" @click="handleSubmit">提交问卷</el-button>
          <el-button size="large" @click="handleCancel">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const questionnaire = ref({})
const questions = ref([])
const formRef = ref()
const form = ref({
  answers: {}
})

const loadQuestionnaire = async () => {
  loading.value = true
  try {
    const res = await request.get(`/api/questionnaire/${route.params.id}`)
    if (res.code === 200) {
      questionnaire.value = res.data
      questions.value = res.data.questions || []
    } else {
      ElMessage.error(res.message || '获取问卷失败')
    }
  } catch (error) {
    console.error('获取问卷失败:', error)
    ElMessage.error('获取问卷失败')
  } finally {
    loading.value = false
  }
}

const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    const res = await request.post(`/api/questionnaire/submit/${route.params.id}`, {
      answers: form.value.answers
    })
    if (res.code === 200) {
      ElMessage.success('提交成功')
      router.push('/home')
    } else {
      ElMessage.error(res.message || '提交失败')
    }
  } catch (error) {
    console.error('提交问卷失败:', error)
    ElMessage.error('提交失败')
  }
}

const handleCancel = () => {
  router.back()
}

onMounted(() => {
  loadQuestionnaire()
})
</script>

<style lang="scss" scoped>
.questionnaire-fill {
  max-width: 800px;
  margin: 20px auto;
  padding: 0 20px;

  .header {
    text-align: center;
    padding: 20px 0;

    .title {
      font-size: 24px;
      font-weight: bold;
      color: #303133;
      margin: 0 0 16px;
    }

    .description {
      font-size: 14px;
      color: #606266;
      line-height: 1.6;
      margin: 0 0 20px;
    }

    .divider {
      height: 1px;
      background: #ebeef5;
      margin: 0 -20px;
    }
  }

  .question-form {
    padding: 20px 0;

    .question-card {
      margin-bottom: 20px;
      border-radius: 8px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
      }

      .question-header {
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        gap: 12px;

        .question-index {
          background: #409EFF;
          color: white;
          padding: 4px 8px;
          border-radius: 4px;
          font-size: 14px;
          font-weight: bold;
        }

        .question-title {
          font-size: 16px;
          color: #303133;
          font-weight: 500;

          .required {
            color: #F56C6C;
            margin-left: 4px;
          }
        }
      }

      .option-group {
        display: flex;
        flex-direction: column;
        gap: 12px;

        .option-item {
          margin: 0;
          padding: 12px 20px;
          border-radius: 4px;
          transition: all 0.3s;

          &:hover {
            background-color: #f5f7fa;
          }

          &.is-checked {
            background-color: #ecf5ff;
            border-color: #409EFF;
          }
        }
      }

      :deep(.el-textarea__inner) {
        padding: 12px;
        font-size: 14px;
        border-radius: 4px;
        
        &:focus {
          border-color: #409EFF;
        }
      }
    }
  }

  .form-footer {
    margin-top: 40px;
    text-align: center;
    
    .el-button {
      min-width: 120px;
      margin: 0 10px;
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .questionnaire-fill {
    padding: 0 10px;

    .header {
      .title {
        font-size: 20px;
      }
    }

    .question-card {
      .question-header {
        .question-title {
          font-size: 14px;
        }
      }
    }
  }
}
</style> 